package com.zzx.test.singleton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * JDBC连接器
 */
public class JDBCConnector {

    private volatile static Connection connection = null;
    private final static String url = "jdbc:mysql://localhost:3306/other_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    private final static String username = "root";
    private final static String password = "root";

    private JDBCConnector() {
    }

    public static Connection getConnection() throws SQLException {
        if (connection == null) {
            synchronized (JDBCConnector.class) {
                // 加载MySql的驱动类
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                } catch (ClassNotFoundException e) {
                    System.out.println("找不到驱动程序类 ，加载驱动失败！");
                    e.printStackTrace();
                }
                if (connection == null) {
                    connection = DriverManager.getConnection(url, username, password);
                }
            }
        }
        return connection;
    }

}
